package com.aloha.springframework.aop.aspectj;

import cn.hutool.core.lang.Assert;
import com.aloha.springframework.aop.ProxyMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.SourceLocation;
import org.aspectj.runtime.internal.AroundClosure;

/**
 * @author DaiZhiHeng
 * @description 使用AspectJ的连接点的实现类，Spring中的连接点实现类是ReflectiveMethodInvocation, 这里会组合Spring中的实现
 * @date 2023/8/2 21:48
 */
public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint {

    private final ProxyMethodInvocation methodInvocation;

    public MethodInvocationProceedingJoinPoint(ProxyMethodInvocation methodInvocation) {
        Assert.notNull(methodInvocation, "MethodInvocation must not be null");
        this.methodInvocation = methodInvocation;
    }

    @Override
    public void set$AroundClosure(AroundClosure aroundClosure) {
        throw new RuntimeException("不支持");
    }

    @Override
    public Object proceed() throws Throwable {
        return methodInvocation.proceed();
    }

    @Override
    public Object proceed(Object[] objects) throws Throwable {
        if (methodInvocation.getArguments().length != objects.length) {
            throw new RuntimeException("参数缺失");
        }
        methodInvocation.setArguments(objects);
        return methodInvocation.proceed();
    }

    @Override
    public String toShortString() {
        return null;
    }

    @Override
    public String toLongString() {
        return null;
    }

    @Override
    public Object getThis() {
        return methodInvocation.getProxy();
    }

    @Override
    public Object getTarget() {
        return methodInvocation.getThis();
    }

    @Override
    public Object[] getArgs() {
        return methodInvocation.getArguments();
    }

    @Override
    public Signature getSignature() {
        return null;
    }

    @Override
    public SourceLocation getSourceLocation() {
        return null;
    }

    @Override
    public String getKind() {
        return null;
    }

    @Override
    public StaticPart getStaticPart() {
        return null;
    }
}
